<?php
namespace app\admin\controller\system;
use app\common\annotation\NodeAnnotation;
use app\common\controller\Admin;
use app\common\traits\ControllerCurd;
use think\Request;
/**
 * @package app\admin\controller\system
 * @NodeAnnotation(
 *     title="广告管理",
 *     is_menu=0,
 *     is_view=0
 * )
 */
class Ad extends Admin
{
    use ControllerCurd;
    public $model;

    public function initialize()
    {
        parent::initialize();
        $this->model= new \app\admin\model\system\Ad();
    }

    /**
     * @NodeAnnotation(
     *     title="广告列表",
     *     parent="admin/system.ad",
     *     MenuRoot="admin/system.setting"
     * )
     */
    public function index(Request $request)
    {
        $keywords  = $request->param("keywords");

        $where = [];

        if($keywords) array_push($where,['title',"like","%$keywords%"]);
        $this->assign([
            "adList" => $this->model->where($where)->paginate(),
            "search" => [
                "keywords" => $keywords
            ]
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="添加广告",
     *     parent="admin/system.ad",
     *     MenuRoot="admin/system.setting",
     *     is_menu=0
     * )
     */
    public function addad(Request $request)
    {
        if($request->isAjax())
        {
            $input = $request->post();
            if(isset($input['datetime']) and !empty($input['datetime']))
            {
                $datetime = explode("~",$input['datetime']);
                $input['start_time'] = strtotime($datetime[0]);
                $input['end_time'] = strtotime($datetime[1]);
            }
            $res = $this->model->create($input);
            if($res)
            {
                $this->success("添加成功",url('admin/system.ad/index'));
            }else{
                $this->error("添加失败",url('admin/system.ad/index'));
            }
        }
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="编辑广告",
     *     parent="admin/system.ad",
     *     MenuRoot="admin/system.setting",
     *     is_menu=0
     * )
     */
    public function editad(Request $request,$id=0)
    {
        if($request->isAjax())
        {
            $input = $request->post();
            if(isset($input['datetime']) and !empty($input['datetime']))
            {
                $datetime = explode("~",$input['datetime']);
                $input['start_time'] = strtotime($datetime[0]);
                $input['end_time'] = strtotime($datetime[1]);
            }
            $res = $this->model->update($input);
            if($res)
            {
                $this->success("修改成功",url('admin/system.ad/index'));
            }else{
                $this->error("修改失败",url('admin/system.ad/index'));
            }
        }
        $this->assign([
            "ad" => $this->model->findData($id)
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="删除广告",
     *     parent="admin/system.ad",
     *     MenuRoot="admin/system.setting",
     *     is_view=0,
     *     is_menu=0
     * )
     */
    public function delad(Request $request)
    {
        $this->del();
    }

}